Skip to content

ocp: fix to free hwcomp log desc memory allocated#2690

Merged
igaw merged 1 commit intolinux-nvme:masterfrom
ikegami-t:ocp-hwcomp-free
Feb 7, 2025
Merged

ocp: fix to free hwcomp log desc memory allocated#2690
igaw merged 1 commit intolinux-nvme:masterfrom
ikegami-t:ocp-hwcomp-free

Conversation

@ikegami-t
Copy link
Contributor

Since the desc pointer used cleanup_free only set NULL value.

if (ret)
print_info_error("error: ocp: failed get hwcomp log: %02X data, ret: %d\n",
OCP_LID_HWCOMP, ret);
return ret;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the command fails, get_hwcomp_log_data is in charge to free any allocated buffer. That means here the function should just return and after osp_show_hwcomp_log just always free the buffer. Note, free has a built in NULL check, so if (ptr) free(ptr) is not necessary anymore.

Copy link
Contributor Author

@ikegami-t ikegami-t Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function nvme_get_log_page() called in get_hwcomp_log_data() but if failed the call then the allocated log->desc not freed so needed to free by the caller or the error return point.

static int get_hwcomp_log_data(struct nvme_dev *dev, struct hwcomp_log *log)
{
...
	log->desc = calloc(1, args.len);
	if (!log->desc) {
		fprintf(stderr, "error: ocp: calloc: %s\n", strerror(errno));
		return -1;
	}

	args.log = log->desc,
	args.lpo = desc_offset,

#ifdef HWCOMP_DUMMY
	memcpy(log->desc, &hwcomp_dummy[desc_offset], args.len);
#else /* HWCOMP_DUMMY */
	ret = nvme_get_log_page(dev_fd(dev), NVME_LOG_PAGE_PDU_SIZE, &args);
	if (ret) {
		print_info_error("error: ocp: failed to get log page (hwcomp: %02X, ret: %d)\n",
				 OCP_LID_HWCOMP, ret);
		return ret;
	}
#endif /* HWCOMP_DUMMY */

Note, free has a built in NULL check, so if (ptr) free(ptr) is not necessary anymore.

That is right. Thank you.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A well behaving function should not change anything on error. So if a function allocates something and then fails later on the function is responsible to free the allocated resources. On the other hand if the function was successful and returns some resources to the caller, the caller is responsible for the resource now.

This makes the error handling on the caller side way more sane.

In the above code path, the correct thing is

	ret = nvme_get_log_page(dev_fd(dev), NVME_LOG_PAGE_PDU_SIZE, &args);
	if (ret) {
		print_info_error("error: ocp: failed to get log page (hwcomp: %02X, ret: %d)\n",
				 OCP_LID_HWCOMP, ret);
		free(log->desc);
		return ret;
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood well. Thank you for your advice and update the patch.

@ikegami-t
Copy link
Contributor Author

Just only fixed to free without the NULL pointer checking at first.

Since the desc pointer used _cleanup_free_ only set NULL value.

Signed-off-by: Tokunori Ikegami <ikegami.t@gmail.com>
[wagi: free buffer on error in get_hwcomp_log_data]
Signed-off-by: Daniel Wagner <wagi@kernel.org>
@igaw
Copy link
Collaborator

igaw commented Feb 7, 2025

I've updated get_hwcomp_log_data so it frees the allocated buffer on error. So the only thing left is to free the buffer in the caller.

@igaw igaw merged commit cb0e18e into linux-nvme:master Feb 7, 2025
17 checks passed
@igaw
Copy link
Collaborator

igaw commented Feb 7, 2025

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants